package com.admin.business.voteVotingRecords;

import com.admin.business.voteEssentialInformation.VoteEssentialInformationDao;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 投票记录表
 * @create: 2022-07-08 19:04:49
 **/
@Service("voteVotingRecords")
@Slf4j
public class VoteVotingRecordsService {

    @Resource
    VoteVotingRecordsDao voteVotingRecordsDao;


    @Resource
    private VoteEssentialInformationDao voteEssentialInformationDao;

    /**
     * 根据id，更新投票记录表数据
     *
     * @param cdoRequest - id:
     *                   - statute_id:规约文件id
     *                   - user_id:投票人id
     *                   - user_name:投票人名称
     *                   - user_building_number:投票人楼号
     *                   - record_state:投票状态：1、同意，2、不同意
     *                   - record_reason:投票原因
     *                   - update_time:更新时间
     *                   - begin_time:创建时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return updateVoteVotingRecordsById(CDO cdoRequest) {
        int affectedRows = voteVotingRecordsDao.updateVoteVotingRecordsById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除投票记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteVotingRecordsById(CDO cdoRequest) {
        int affectedRows = voteVotingRecordsDao.deleteVoteVotingRecordsById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除投票记录表数据
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteVotingRecords(CDO cdoRequest) {
        int affectedRows = voteVotingRecordsDao.deleteVoteVotingRecords(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取投票记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingRecordsById(CDO cdoRequest) {
        CDO cdoMapperResult = voteVotingRecordsDao.getVoteVotingRecordsById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取投票记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingRecords(CDO cdoRequest) {
        CDO cdoMapperResult = voteVotingRecordsDao.getVoteVotingRecords(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取投票记录表数据列表
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingRecordsList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteVotingRecordsDao.getVoteVotingRecordsList(cdoRequest);
        if (cdoMapperResultList.size()>0){
            for (CDO cdo : cdoMapperResultList) {
                if (cdo.getString("user_id")!=null&&!"".equals(cdo.getString("user_id"))){
                    CDO sysUserInfoByUserId = voteEssentialInformationDao.getSysUserInfoByUserId(cdo);
                    if (sysUserInfoByUserId!=null){
                        cdo.set("user_head_portrait",sysUserInfoByUserId.getString("user_head_portrait"));
                        cdo.set("user_front_head_portrait",sysUserInfoByUserId.getString("user_front_head_portrait"));
                    }
                }
            }
        }
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取投票记录表数据列表
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteVotingRecordsListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteVotingRecordsDao.getVoteVotingRecordsListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteVotingRecordsDao.getVoteVotingRecordsListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
